
import TaskStep from '@/components/taskStep.vue';

// 请求通用方法
import apiMixin from '@/mixin/apiMixin';
// 任务通用方法
import taskMixin from '@/mixin/taskMixin';

import keyup from '@/mixin/keyup';

import uilts from '@/utils/index';
export default {
    mixins: [apiMixin,taskMixin,keyup],
    data() {
        return {
            loading: false,
            saveBtnLoad: false,
            taskLink: {
                total_surplus_accepts: 0,
                total_accepts: 0,
                diffculty_level: 1,
                limited_os: []
            },
            taskSteps: [],
            taskLevel: 1,
            mode: '',
            listPage: 1,
            navIdList: [],
            affirmBtnLoad: false,
            dynamicTags: [],
            inputVisible: false,
            inputValue: ''
        }
    },
    mounted() {
        this.getTaskIdList();
        this.init();
    },
    methods: {
        init() {
            let taskId = this.$route.query.taskId
            this.getTaskDetailApi(taskId);
        },
        getTaskDetailApi(taskId) {
            let _self = this;
            this.comResquest(this.request.getTaskDetail,(res)=>{
                let stpes = res.custom_steps;
                delete res.custom_steps;
                _self.taskSteps = stpes;
                _self.parseTaskLink(res);
                _self.dynamicTags = res.tag;
            },{
                id: taskId,
                load: true
            });
        },
        parseTaskLink(taskLink) {
            this.taskLink = taskLink;
        },
        handleCheckedCitiesChange(value) {
            console.log(value);
        },
        handleEdit() {
            let _self = this;
            console.log(this.taskSteps);
            //this.changeTaskSpecStatus();
            //this.updateTaskDetailApi();
            _self.saveBtnLoad = true;
            Promise.all([this.changeTaskSpecStatus(),this.updateTaskDetailApi()]).then(function(values) {
                _self.saveBtnLoad = false;
                _self.init();
                _self.$message.success("修改成功");
            }).catch((ero)=>{
                _self.saveBtnLoad = false;
                console.log(ero);
            })
        },
        updateTaskDetailApi() {
            let taskId = this.$route.query.taskId
            let taskLink =  uilts.simpleCopy(this.taskLink);
            taskLink.custom_steps = JSON.stringify(this.taskSteps);
            taskLink.bids = this.uploadBids;
            taskLink.tag = JSON.stringify(this.dynamicTags);
            let _self = this;
            return new Promise(function(resolve, reject) {
                _self.comResquest(_self.request.updateTaskDetail,(res)=>{
                    resolve(1);
                },{
                    id: taskId,
                    update_data: taskLink,
    
                },(ero)=>{
                    reject(ero);
                });
            });
        },
        changeTaskSpecStatus() {
            let _self = this;
            let taskId = this.$route.query.taskId;
            let taskLink =  uilts.simpleCopy(this.taskLink);
            let resPar = {
                id: taskId
            };
            resPar.is_top = taskLink.is_top;
            resPar.is_recommand = taskLink.is_recommand;
            return new Promise(function(resolve, reject) {
                _self.comResquest(_self.request.postTaskList,(res)=>{
                    resolve(2);
                },resPar,(ero)=>{
                    reject(ero);
                })
            });
        },
        handleClearing(status) {
            let taskId = this.$route.query.taskId;
            let _self = this;
            let hint = status == 1 ? '清算任务' : '取消清算';
            this.$confirm( hint + ', 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
                center: true
            }).then(() => {
                _self.comResquest(_self.request.repayTaskById,(res)=>{
                    _self.$message.success(hint + "成功");
                    _self.init();
                },{
                    taskId: taskId,
                    status: status,
                });
            }).catch(() => {
                
            });
        },
        getTaskIdList() {
            this.getIdListApi(this.request.getTaskList);
        },
        hanldeQuiteNav(type) {
            let taskId = this.$route.query.taskId;
            this.comQuiteNav(type,taskId,(changeId)=>{
                this.$router.push({ query: { taskId: changeId,listPage: this.listPage } });
                this.init();
            });
        },
        switchPage() {
            return new Promise((resolve,reject)=>{
                let _self = this;
                this.comResquest(this.request.getTaskList,(res)=>{
                    resolve(res);
                },{
                    load: true,
                    page: this.listPage,
                    id_only: 1,
                    pagesize: 10
                })
            })
        },
        handleClose(tag) {
            this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1);
        },
    
        showInput() {
            this.inputVisible = true;
            this.$nextTick(_ => {
                this.$refs.saveTagInput.$refs.input.focus();
            });
        },
        handleInputConfirm() {
            let inputValue = this.inputValue;
            if (inputValue) {
                this.dynamicTags.push(inputValue);
            }
            this.inputVisible = false;
            this.inputValue = '';
        }
    },
    components:{
        TaskStep
    },
}